package com.ruoyi.project.wms.essentials.storage.controller;

import java.util.List;

import com.ruoyi.project.wms.essentials.purchase.domain.WmsPurchase;
import com.ruoyi.project.wms.essentials.purchase.service.IWmsPurchaseService;
import com.ruoyi.project.wms.essentials.storage.domain.WmsStorage;
import com.ruoyi.project.wms.essentials.storage.service.IWmsStorageService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 入库记录Controller
 * 
 * @author ruoyi
 * @date 2020-10-29
 */
@Controller
@RequestMapping("/essentials/storage")
public class WmsStorageController extends BaseController
{
    private String prefix = "wms/essentials/storage";

    @Autowired
    private IWmsStorageService wmsStorageService;
    @Autowired
    private IWmsPurchaseService wmsPurchaseService;

    @RequiresPermissions("essentials:storage:view")
    @GetMapping()
    public String storage()
    {
        return prefix + "/storage";
    }

    /**
     * 查询入库记录列表
     */
    @RequiresPermissions("essentials:storage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmsStorage wmsStorage)
    {
        startPage();
        List<WmsStorage> list = wmsStorageService.selectWmsStorageList(wmsStorage);
        return getDataTable(list);
    }

    /**
     * 导出入库记录列表
     */
    @RequiresPermissions("essentials:storage:export")
    @Log(title = "入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmsStorage wmsStorage)
    {
        List<WmsStorage> list = wmsStorageService.selectWmsStorageList(wmsStorage);
        ExcelUtil<WmsStorage> util = new ExcelUtil<WmsStorage>(WmsStorage.class);
        return util.exportExcel(list, "storage");
    }

    /**
     * 新增入库记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存入库记录
     */
    @RequiresPermissions("essentials:storage:add")
    @Log(title = "入库记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmsStorage wmsStorage)
    {
        return toAjax(wmsStorageService.insertWmsStorage(wmsStorage));
    }

    /**
     * 修改入库记录
     */
    @GetMapping("/edit/{stoId}")
    public String edit(@PathVariable("stoId") Long stoId, ModelMap mmap)
    {
        WmsStorage wmsStorage = wmsStorageService.selectWmsStorageById(stoId);
        mmap.put("wmsStorage", wmsStorage);
        return prefix + "/edit";
    }

    /**
     * 修改保存入库记录
     */
    @RequiresPermissions("essentials:storage:edit")
    @Log(title = "入库记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmsStorage wmsStorage)
    {
        return toAjax(wmsStorageService.updateWmsStorage(wmsStorage));
    }

    /**
     * 删除入库记录
     */
    @RequiresPermissions("essentials:storage:remove")
    @Log(title = "入库记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wmsStorageService.deleteWmsStorageByIds(ids));
    }

    /**
     * 跳转商品入库页面
     */
    @RequiresPermissions("essentials:storage:view")
    @RequestMapping("/pursto")
    public String PurStorage(){
        return prefix + "/pursto";
    }

    /**
     * 查询订货单记录表
     */
    @RequiresPermissions("essentials:purchase:list")
    @PostMapping("/pursto/list")
    @ResponseBody
    public TableDataInfo disposeList(WmsPurchase wmsPurchase){
        startPage();
        wmsPurchase.setPurStatus("已提交");
        List<WmsPurchase>list =wmsPurchaseService.selectWmsPurchaseList(wmsPurchase);
        return getDataTable(list);
    }

    /**
     * 根据订货单id完成入库
     */
    @ResponseBody
    @PostMapping("/stofinish")
    public Boolean disposeSub(Integer purId){
        int i = wmsStorageService.insertPro(purId);
        return i>0;
    }
}
